package com.rfid.afd.model;

import android.graphics.RectF;

import com.rfid.afd.activity.view.IGatherView;
import com.rfid.config.ConfigManager;
import com.rfid.db.bean.Gather;
import com.rfid.util.DBManager;
import com.rfid.util.LogUtils;
import com.rfid.util.UserManager;
import com.rfid.util.Util;

public class GatherModel extends BaseModel {

    private String mShelf;
    @Override
    public void onRfid(String rfid) {
        //decodeEpc(rfid);
        super.onRfid(rfid);
    }

    private String decodeEpc(String rfid) {
        try {
            String hex = rfid.substring(4 * 2, rfid.length());
            byte data[] = Util.hexStr2bytes(hex);
            int r = 0;
            if (hex.length() > 16) {
                r = mRfidDecoder.iRFID_Decode128bit(data);
            } else {
                r = mRfidDecoder.iRFID_Decode96bit(data);
            }
            if(r <= 0) {
                LogUtils.d("decode fail rfid = " + rfid + ",r = " + r);
                return null;
            }
            String barcode = new String(data).trim();
            if(barcode.length() == ConfigManager.getInstance().getConfig().getShelfLabLen()) {
                if(mShelf != null) {
                    LogUtils.d("repeat shelf = " + rfid);
                    return null;
                }
                mShelf = barcode;
                if(mRfidListener != null) {
                    mRfidListener.onShelf(barcode);
                }
            } else {
                if(mRfidListener != null) {
                    mRfidListener.onRfid(rfid,barcode);
                }
            }
            return barcode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void gather(String shelf,String uid,String barcode) {
        Gather gather = new Gather();
        gather.setBarcode(barcode);
        gather.setUid(uid);
        gather.setOptime(Util.getDateTime());
        gather.setShelf(shelf);
        gather.setUser(UserManager.getInstance().getLoginUser().getName());
        DBManager.addGather(gather);
    }
}
